package main

import (
	"context"
	"flag"
	"fmt"
	"google.golang.org/grpc"
	"log"
	"net"
	leaderboard "rankinglist/config"
	log2 "rankinglist/log"
	"rankinglist/service"
)

var rankAddr = flag.Int("rankAddr", 16013, "the address to connect to")

func main() {
	flag.Parse()
	lis, err := net.Listen("tcp", fmt.Sprintf("rankinglist:%d", *rankAddr))
	if err != nil {
		writeLog, err2 := log2.WriteLog(context.Background(), "ranklist", "GRPC server", "ranklist GRPC server", "Error", err.Error())
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}
		log.Fatalf("failed to listen: %v", err)
		//panic()
	}
	s := grpc.NewServer()
	leaderboard.RegisterProdScoreBoardServiceServer(s, &service.Rank{})
	log.Printf("server listening at %v", lis.Addr())
	if err := s.Serve(lis); err != nil {
		writeLog, err2 := log2.WriteLog(context.Background(), "ranklist", "GRPC server", "ranklist GRPC server", "Error", err.Error())
		if writeLog == 200 {
			fmt.Println("成功写入日志")
		}
		if err2 != nil {
			fmt.Println("写入日志失败")
		}
		log.Fatalf("failed to serve: %v", err)
	}
	/*log.Println("start")
	c := cron.New(cron.WithSeconds())
	spec := "00 * * * * ?"
	_, err = c.AddFunc(spec, database.MigrationDataToMysql)
	if err != nil {
		log.Println(err.Error())
		return
	}
	c.Start()
	defer c.Stop()*/
}
